const cacheScrollName = 'qmp-cache-scroll-name'
const commonData = {
  namespaced: true,
  state: {
    userInfo: null, // 用户信息
    isOnQuanUBase: false, // 是否在移动应用平台app中
    headerBarHeight: 0, // h5头部导航栏高度
    keepAlivePages: []
  },
  mutations: {
    updateUserInfo(state: { userInfo: unknown }, data: unknown): void {
      state.userInfo = data
    },
    updateQuanUBase(state: { isOnQuanUBase: boolean }, data: boolean): void {
      state.isOnQuanUBase = data
    },
    updateHeaderBarHeight(state: { headerBarHeight: number }, data: number): void {
      state.headerBarHeight = data
    },
    // 添加单个页面缓存
    addKeepAlivePage(state: { keepAlivePages: string[] }, pageName: string) {
      state.keepAlivePages.push(pageName)
    },
    // 删除单个页面缓存
    deleteKeepAlivePage(state: { keepAlivePages: string[] }, pageName: string) {
      state.keepAlivePages = state.keepAlivePages.filter((name) => name !== pageName)
      // 删除滚动记录
      let scrollRecord = sessionStorage.getItem(cacheScrollName)
      if (scrollRecord) {
        let scrollRecordMap: {
          [key: string]: any
        } = {}
        scrollRecordMap = JSON.parse(scrollRecord)
        delete scrollRecordMap[pageName]
        sessionStorage.setItem(cacheScrollName, JSON.stringify(scrollRecordMap))
      }
    },
    // 删除所有页面缓存
    deleteAllKeepAlivePage(state: { keepAlivePages: string[] }) {
      state.keepAlivePages = []
      sessionStorage.removeItem(cacheScrollName)
    }
  },
  getters: {
    userInfo(state: { userInfo: unknown }): unknown {
      return state.userInfo
    },
    isOnQuanUBase(state: { isOnQuanUBase: boolean }): boolean {
      return state.isOnQuanUBase
    },
    headerBarHeight(state: { headerBarHeight: number }): number {
      return state.headerBarHeight
    },
    keepAlivePages(state: { keepAlivePages: string[] }): string[] {
      return state.keepAlivePages
    }
  }
}
export default commonData
